<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://java.sun.com/jsf/facelets"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.org/ui"
                xmlns:of="http://omnifaces.org/functions"
                template="/templates/mainLayout.xhtml"
                xmlns:f="http://java.sun.com/jsf/core">
    
    <ui:define name="title">Quản lý file</ui:define>
    <ui:define name="content">
        <h:form id="uploadForm" enctype="multipart/form-data">
            <p:dialog header="Upload File" widgetVar="uploadFileDialog" resizable="false">
                <p:fileUpload fileUploadListener="#{fileManagerBean.uploadFileAction}"  
                              mode="advanced"
                              update=":mainForm"
                              multiple="true"
                              sizeLimit="2000000"
                              oncomplete="uploadFileDialog.hide()"/>
            </p:dialog>
        </h:form>
        
        <h:form id="newForm">
            <p:dialog header="Tạo mới" widgetVar="newDialog" resizable="false">
                <h:panelGrid styleClass="panelGrid" columnClasses="ui-state-default labelCol, ui-widget-content" 
                             columns="2" cellpadding="5" width="300px">                                         
                    <h:outputLabel for="name" style="display: inline; float: right; padding-right: 5px;" value="Tên *"/>
                    <p:inputText id="name" value="#{fileManagerBean.newName}" maxlength="100" required="true"
                                 label="Tên" requiredMessage="Yêu cầu nhập tên"/>

                    <h:outputLabel for="fileType" style="display: inline; float: right; padding-right: 5px;" value="Loại *"/>
                    <p:selectOneRadio id="fileType" value="#{fileManagerBean.newFileType}">
                        <f:selectItem itemLabel="File" itemValue="File"/>
                        <f:selectItem itemLabel="Thư mục" itemValue="Directory"/>
                    </p:selectOneRadio>
                    
                    <h:outputLabel value=""/>
                    <p:commandButton value="Lưu" update=":mainForm" oncomplete="newDialog.hide()"
                                     actionListener="#{fileManagerBean.createAction()}"/>
                </h:panelGrid>
            </p:dialog>
        </h:form>

        <h:form id="renameForm">
            <p:dialog header="Đổi tên" widgetVar="renameDialog" resizable="false">
                <h:panelGrid styleClass="panelGrid" columnClasses="ui-state-default labelCol, ui-widget-content" 
                             columns="2" cellpadding="5" width="300px">                                         
                    <h:outputLabel for="name" style="display: inline; float: right; padding-right: 5px;" value="Tên mới *"/>
                    <p:inputText id="name" value="#{fileManagerBean.newName}" maxlength="100" required="true"
                                 label="Tên mới" requiredMessage="Yêu cầu nhập tên mới"/>

                    <h:outputLabel value=""/>
                    <p:commandButton value="Lưu" update=":mainForm" oncomplete="renameDialog.hide()"
                                     actionListener="#{fileManagerBean.renameAction()}"/>
                </h:panelGrid>
            </p:dialog>
        </h:form>
        
        <h:form id="downloadLinkForm">
            <p:dialog header="Download Link" widgetVar="downloadLinkDialog" resizable="false">
                <h:panelGrid styleClass="panelGrid" columnClasses="ui-state-default labelCol, ui-widget-content" 
                             columns="2" cellpadding="5" width="300px">                                         
                    <h:outputText value="#{fileManagerBean.getDownloadLink()}"/>
                </h:panelGrid>
            </p:dialog>
        </h:form>

        <h:form id="moveForm">
            <p:dialog header="Chuyển" widgetVar="moveDialog" resizable="false">
                <h:panelGrid styleClass="panelGrid" columnClasses="ui-state-default labelCol, ui-widget-content" 
                             columns="2" cellpadding="5" width="300px">                                         
                    <h:outputLabel for="path" style="display: inline; float: right; padding-right: 5px;" value="Vị trí mới *"/>
                    <p:inputText id="path" value="#{fileManagerBean.newPath}" maxlength="100" required="true"
                                 label="Vị trí mới" requiredMessage="Yêu cầu nhập vị trí mới"/>

                    <h:outputLabel value=""/>
                    <p:commandButton value="Chuyển" update=":mainForm" oncomplete="moveDialog.hide()"
                                     actionListener="#{fileManagerBean.moveAction()}"/>
                </h:panelGrid>
            </p:dialog>
        </h:form>

        <h:form id="copyForm">
            <p:dialog header="Sao chép" widgetVar="copyDialog" resizable="false">
                <h:panelGrid styleClass="panelGrid" columnClasses="ui-state-default labelCol, ui-widget-content" 
                             columns="2" cellpadding="5" width="300px">                                         
                    <h:outputLabel for="path" style="display: inline; float: right; padding-right: 5px;" value="Vị trí *"/>
                    <p:inputText id="path" value="#{fileManagerBean.newPath}" maxlength="100" required="true"
                                 label="Vị trí" requiredMessage="Yêu cầu nhập vị trí"/>

                    <h:outputLabel value=""/>
                    <p:commandButton value="Sao chép" update=":mainForm" oncomplete="copyDialog.hide()"
                                     actionListener="#{fileManagerBean.copyAction()}"/>
                </h:panelGrid>
            </p:dialog>
        </h:form>
        
        <h:form id="deleteForm">
            <p:confirmDialog message="Bạn có muốn file/thư mục này?" appendToBody="true"
                                 header="Thông báo" severity="alert" widgetVar="deleteConfirm">  
                    <p:commandButton value="Có" oncomplete="deleteConfirm.hide()" update=":mainForm"
                                     actionListener="#{fileManagerBean.deleteAction()}" />  
                    <p:commandButton value="Không" onclick="deleteConfirm.hide()" type="button" />                     
                </p:confirmDialog>
        </h:form>
        
        <h:form id="mainForm">
            <p:messages id="msgs"/>
                
            <p:contextMenu for="fileList">
                <p:menuitem value="New" icon="ui-icon-newwin" oncomplete="newDialog.show()"/>
                <p:menuitem value="Copy" icon="ui-icon-copy" actionListener="#{fileManagerBean.preCopyAction()}" update=":mainForm:msgs :copyForm" oncomplete="handleCopyRequest(xhr, status, args)"/>
                <p:menuitem value="Move" icon="ui-icon-transfer-e-w" actionListener="#{fileManagerBean.preMoveAction()}" update=":mainForm:msgs :moveForm" oncomplete="handleMoveRequest(xhr, status, args)"/>
                <p:menuitem value="Delete" icon="ui-icon-trash" actionListener="#{fileManagerBean.preDeleteAction()}" update=":mainForm:msgs" oncomplete="handleDeleteRequest(xhr, status, args)"/>
                <p:menuitem value="Rename" icon="ui-icon-pencil" actionListener="#{fileManagerBean.preRenameAction()}" update=":mainForm:msgs :renameForm" oncomplete="handleRenameRequest(xhr, status, args)"/>
                <p:menuitem value="Link File" icon="ui-icon-arrowthick-1-s" actionListener="#{fileManagerBean.preGetDownloadLinkAction()}" update=":mainForm:msgs :downloadLinkForm" oncomplete="handleGetDownloadLinkRequest(xhr, status, args)"/>
                <p:menuitem value="Upload" icon="ui-icon-arrowthick-1-n" oncomplete="uploadFileDialog.show()"/>
                <p:menuitem value="Reload" icon="ui-icon-refresh" actionListener="#{fileManagerBean.reloadAction()}" update=":mainForm"/>
            </p:contextMenu>
                
            <p:panel header="File Manager">
                <p:menubar style="margin-bottom: 10px;">
                    <p:menuitem value="New" icon="ui-icon-newwin" oncomplete="newDialog.show()"/>
                    <p:menuitem value="Copy" icon="ui-icon-copy" actionListener="#{fileManagerBean.preCopyAction()}" update=":mainForm:msgs :copyForm" oncomplete="handleCopyRequest(xhr, status, args)"/>
                    <p:menuitem value="Move" icon="ui-icon-transfer-e-w" actionListener="#{fileManagerBean.preMoveAction()}" update=":mainForm:msgs :moveForm" oncomplete="handleMoveRequest(xhr, status, args)"/>
                    <p:menuitem value="Delete" icon="ui-icon-trash" actionListener="#{fileManagerBean.preDeleteAction()}" update=":mainForm:msgs" oncomplete="handleDeleteRequest(xhr, status, args)"/>
                    <p:menuitem value="Rename" icon="ui-icon-pencil" actionListener="#{fileManagerBean.preRenameAction()}" update=":mainForm:msgs :renameForm" oncomplete="handleRenameRequest(xhr, status, args)"/>
                    <p:menuitem value="Link File" icon="ui-icon-arrowthick-1-s" actionListener="#{fileManagerBean.preGetDownloadLinkAction()}" update=":mainForm:msgs :downloadLinkForm" oncomplete="handleGetDownloadLinkRequest(xhr, status, args)"/>
                    <p:menuitem value="Upload" icon="ui-icon-arrowthick-1-n" oncomplete="uploadFileDialog.show()"/>
                    <p:menuitem value="Reload" icon="ui-icon-refresh" actionListener="#{fileManagerBean.reloadAction()}" update=":mainForm"/>
                </p:menubar>
                    
                <p:treeTable value="#{fileManagerBean.root}" var="file" id="fileList"   
                             selectionMode="single" selection="#{fileManagerBean.selectedNode}">  
                    <f:facet name="header">
                        File Manager
                    </f:facet>  
                    <p:column>  
                        <f:facet name="header">  
                            Name
                        </f:facet>  
                        <h:outputText value="#{file.name}" />  
                    </p:column>  
                        
                    <p:column>  
                        <f:facet name="header">  
                            Size  
                        </f:facet>  
                        <h:outputText value="#{of:formatBytes(file.length())}" rendered="#{file.isFile()}" />  
                    </p:column>  
                        
                    <p:column>  
                        <f:facet name="header">  
                            Type  
                        </f:facet>  
                        <h:outputText value="File" rendered="#{file.isFile()}" /> 
                        <h:outputText value="Directory" rendered="#{file.isDirectory()}" /> 
                    </p:column>
                        
                    <p:column>  
                        <f:facet name="header">  
                            Modified  
                        </f:facet>  
                        <h:outputText value="#{of:formatDate(fileManagerBean.getLastModified(file), 'yyyy-MM-dd HH:mm:ss')}"/>
                    </p:column>  
                </p:treeTable>
            </p:panel>
        </h:form>
            
        <script type="text/javascript">  
            function start() {  
                statusDialog.show();  
            }  
            
            function stop() {  
                statusDialog.hide();  
            }  
            function handleCopyRequest(xhr, status, args) {
                if(!args.validationFailed &amp;&amp; !args.invalid) {
                    copyDialog.show();
                }
            }  
            function handleMoveRequest(xhr, status, args) {
                if(!args.validationFailed &amp;&amp; !args.invalid) {
                    moveDialog.show();
                }
            }  
            function handleRenameRequest(xhr, status, args) {
                if(!args.validationFailed &amp;&amp; !args.invalid) {
                    renameDialog.show();
                }
            }
            function handleDeleteRequest(xhr, status, args) {
                if(!args.validationFailed &amp;&amp; !args.invalid) {
                    deleteConfirm.show();
                }
            }
            function handleGetDownloadLinkRequest(xhr, status, args) {
                if(!args.validationFailed &amp;&amp; !args.invalid) {
                    downloadLinkDialog.show();
                }
            }
        </script>
    </ui:define>
</ui:composition>